/*
* berton
*/
package com.pitop.t.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.t.entity.User;
import com.pitop.t.service.TuserSV;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
*  控制器
*
* @author berton
*/
@RestController
@RequestMapping("/user")
@Slf4j
//@Api(value = "控制器", description = "控制器")
public class TuserCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private TuserSV tuserSV;



    /**
    * 根据条件userId查询Tuser一个详情信息
    *
    * @param userId
    * @return BeanRet
    */
    @ApiOperation(value = "查询Tuser一个详情信息", notes = "查询Tuser一个详情信息")
    @ApiImplicitParams({
       @ApiImplicitParam(name = "userId", value = "userId",dataType = "java.lang.Integer", paramType = "query")
    })
    //@RequestMapping(value = "/load/userId/{userId}", method = RequestMethod.GET)
    @RequiresPermissions("user:load")
    @RequestMapping(value = "/loadByUserId",method = RequestMethod.GET)
    @ResponseBody
    public User loadByUserId(java.lang.Integer userId) {
        if(userId==null){
           return null;
        }
        User tuser = tuserSV.loadByUserId(userId);
        log.info(JSON.toJSONString(tuser));
        return tuser;
    }


    /**
    * 查询Tuser信息集合
    *
    * @return 分页对象
    */
    @ApiOperation(value = "查询Tuser信息集合", notes = "查询Tuser信息集合")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
    @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
        @ApiImplicitParam(name = "createTimeBegin", value = "", paramType = "query"),
        @ApiImplicitParam(name = "createTimeEnd", value = "", paramType = "query"),
        @ApiImplicitParam(name = "updateTimeBegin", value = "", paramType = "query"),
        @ApiImplicitParam(name = "updateTimeEnd", value = "", paramType = "query")
    })
    @RequiresPermissions("user:count")
    @PostMapping(value = "/list")
    @ResponseBody
    public List<User> list( @ApiIgnore User tuser, Integer curPage, Integer pageSize) {
        Page<User> page=new Page<User>(pageSize,curPage);
        List<User> tusers = tuserSV.list(tuser,page.genRowStart(),page.getPageSize());
        int total = tuserSV.count(tuser);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return tusers;
    }


    /**
    * 查询Tuser信息集合
    *
    * @return 分页对象
    */
    @ApiOperation(value = "查询Tuser信息集合", notes = "查询Tuser信息集合")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
    @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    @ApiImplicitParam(name = "userId", value = "userId", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<User> listByPk(java.lang.Integer userId, Integer curPage, Integer pageSize) {
        Page<User> page=new Page<User>(pageSize,curPage);
        List<User> tusers = tuserSV.list(userId, page.genRowStart(),page.getPageSize());
        int total = tuserSV.count(userId);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return tusers;
    }

    /**
    * 统计Tuser信息数量
    *
    * @return 总条数
    */
    @ApiOperation(value = "统计Tuser信息数量", notes = "统计Tuser信息数量")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "userId", value = "", paramType = "query"),
    @ApiImplicitParam(name = "userName", value = "用户名", paramType = "query"),
    @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),
    @ApiImplicitParam(name = "email", value = "电子邮箱", paramType = "query"),
    @ApiImplicitParam(name = "phone", value = "手机号码", paramType = "query"),
    @ApiImplicitParam(name = "salt", value = "盐", paramType = "query"),
    @ApiImplicitParam(name = "status", value = "状态1:启用 0：禁用", paramType = "query"),
    @ApiImplicitParam(name = "createTime", value = "", paramType = "query"),
    @ApiImplicitParam(name = "updateTime", value = "", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count( User tuser) {
        if(tuser==null){
            return tuserSV.count(new HashMap());
        }else{
            return tuserSV.count(tuser);
        }
    }



    /**
    * 创建Tuser
    *
    * @return BeanRet
    */
    @ApiOperation(value = "创建Tuser", notes = "创建Tuser")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "userName", value = "用户名", paramType = "query"),
    @ApiImplicitParam(name = "password", value = "密码", paramType = "query"),
    @ApiImplicitParam(name = "email", value = "电子邮箱", paramType = "query"),
    @ApiImplicitParam(name = "phone", value = "手机号码", paramType = "query"),
    @ApiImplicitParam(name = "salt", value = "盐", paramType = "query"),
    @ApiImplicitParam(name = "status", value = "状态1:启用 0：禁用", paramType = "query")
    })
    @RequiresPermissions("user:add")
    @PostMapping("/build")
    @ResponseBody
    public User build( @ApiIgnore User tuser) {
        tuserSV.save(tuser);
        return tuser;
    }



}
